package com.smec.moblie.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.smec.moblie.R;
import com.smec.moblie.bean.FeedbackBean;
import com.smec.moblie.bean.FeedbackContentBean;
import com.smec.moblie.bean.SelectListBean;
import com.smec.moblie.common.ConstValue;
import com.smec.moblie.util.DateUtil;
import com.smec.moblie.view.dialog.MyDatePicker;
import com.smec.moblie.view.gridView.MyGridView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sunyubo on 2017/12/18.
 */

public class FeedBackListAdapter extends BaseAdapter implements ConstValue {
    private List<FeedbackBean> feedbackBeanList;
    private LayoutInflater inflater;
    private Context context;
    private String status;

    public FeedBackListAdapter(Context context, String status) {
        feedbackBeanList = new ArrayList<>();
        inflater = LayoutInflater.from(context);
        this.status = status;
        this.context = context;
    }

    public void setList(List<FeedbackBean> feedbackBeanList) {
        this.feedbackBeanList = feedbackBeanList;
        notifyDataSetChanged();
    }

    public List<FeedbackBean> getList() {
        return feedbackBeanList;
    }

    @Override
    public int getCount() {
        return feedbackBeanList.size();
    }

    @Override
    public FeedbackBean getItem(int position) {
        return feedbackBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return feedbackBeanList.get(position).getFeedbackId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = inflater.inflate(R.layout.adapter_feedback_text, null);
        } else {
            view = convertView;
        }
        FeedbackBean feedbackBean = feedbackBeanList.get(position);
        if (feedbackBean.getFeedbackType().equals(TEXT)) {
            view = inflater.inflate(R.layout.adapter_feedback_text, null);
            TextView textView= (TextView) view.findViewById(R.id.tv_must);
            if(feedbackBean.getRequired().equals("Y")){
                textView.setVisibility(View.VISIBLE);
            }
            LinearLayout layoutTitle = (LinearLayout) view.findViewById(R.id.layoutTitle);
            if (position == 0) {
                layoutTitle.setVisibility(View.VISIBLE);
            } else {
                layoutTitle.setVisibility(View.GONE);
            }
            TextView textVieweSquence = (TextView) view.findViewById(R.id.textVieweSquence);
            TextView textViewTitle = (TextView) view.findViewById(R.id.textViewTitle);
            EditText editTextInput = (EditText) view.findViewById(R.id.editTextInput);
            TextView textViewRemark = (TextView) view.findViewById(R.id.textViewRemark);
            LinearLayout layout_remark = (LinearLayout) view.findViewById(R.id.layout_remark);
            textVieweSquence.setText(position + 1 + "");
            textViewTitle.setText(feedbackBean.getTitle());
            if (feedbackBean.getRemark() != null && !feedbackBean.getRemark().equals("")) {
                layout_remark.setVisibility(View.VISIBLE);
                textViewRemark.setText(feedbackBean.getRemark());
            } else {
                layout_remark.setVisibility(View.GONE);
            }
            if (feedbackBean.getFeedbackContent() != null && feedbackBean.getFeedbackContent().getContent() != null) {
                editTextInput.setText(feedbackBean.getFeedbackContent().getContent());
            }
            editTextInput.addTextChangedListener(new MyTextWatcher(feedbackBean));
            editTextInput.setHint("请输入信息...");
            if (status.equals(COMPLETED)) {
                editTextInput.setEnabled(false);
            }
        } else if (feedbackBean.getFeedbackType().equals(IMAGE)) {
            view = inflater.inflate(R.layout.adapter_feedback_image, null);
            LinearLayout layoutTitle = (LinearLayout) view.findViewById(R.id.layoutTitle);
            if (position == 0) {
                layoutTitle.setVisibility(View.VISIBLE);
            } else {
                layoutTitle.setVisibility(View.GONE);
            }
            TextView textVieweSquence = (TextView) view.findViewById(R.id.textVieweSquence);
            TextView textViewTitle = (TextView) view.findViewById(R.id.textViewTitle);
            MyGridView myGridView = (MyGridView) view.findViewById(R.id.myGridView);
            TextView textViewRemark = (TextView) view.findViewById(R.id.textViewRemark);
            LinearLayout layout_remark = (LinearLayout) view.findViewById(R.id.layout_remark);
            textVieweSquence.setText(position + 1 + "");
            textViewTitle.setText(feedbackBean.getTitle());
            if (feedbackBean.getRemark() != null && !feedbackBean.getRemark().equals("")) {
                layout_remark.setVisibility(View.VISIBLE);
                textViewRemark.setText(feedbackBean.getRemark());
            } else {
                layout_remark.setVisibility(View.GONE);
            }
            FeedBackImageAdapter feedBackImageAdapter = new FeedBackImageAdapter(context, position, status);
            List<SelectListBean> selectListBeanList = feedbackBean.getFeedbackContent().getSelectList();
            if (selectListBeanList != null && selectListBeanList.size() > 0) {
                if (selectListBeanList.get(selectListBeanList.size() - 1).getImagePath() != null && selectListBeanList.get(selectListBeanList.size() - 1).getImagePath().equals(ADDString)) {
                    selectListBeanList.remove(selectListBeanList.size() - 1);
                }
            } else {
                selectListBeanList = new ArrayList<>();
            }
            SelectListBean selectListBeanNew = new SelectListBean();
            selectListBeanNew.setFeedbackId(feedbackBean.getFeedbackId());
            selectListBeanNew.setItemId(feedbackBean.getItemId());
            selectListBeanNew.setImagePath(ADDString);
            selectListBeanList.add(selectListBeanNew);
            feedbackBean.getFeedbackContent().setSelectList(selectListBeanList);
            feedBackImageAdapter.setList(selectListBeanList);
            myGridView.setAdapter(feedBackImageAdapter);

        } else {
            view = inflater.inflate(R.layout.adapter_feedback_list, null);
            TextView textView= (TextView) view.findViewById(R.id.tv_must);
            if(feedbackBean.getRequired().equals("Y")){
                textView.setVisibility(View.VISIBLE);
            }
            LinearLayout layoutTitle = (LinearLayout) view.findViewById(R.id.layoutTitle);
            if (position == 0) {
                layoutTitle.setVisibility(View.VISIBLE);
            } else {
                layoutTitle.setVisibility(View.GONE);
            }
            TextView textVieweSquence = (TextView) view.findViewById(R.id.textVieweSquence);
            TextView textViewTitle = (TextView) view.findViewById(R.id.textViewTitle);
            TextView textViewRemark = (TextView) view.findViewById(R.id.textViewRemark);
            ImageView image_icon = (ImageView) view.findViewById(R.id.image_icon);
            LinearLayout layout_remark = (LinearLayout) view.findViewById(R.id.layout_remark);
            TextView textViewContent = (TextView) view.findViewById(R.id.textViewContent);
            RelativeLayout layoutSelector = (RelativeLayout) view.findViewById(R.id.layoutSelector);
            textVieweSquence.setText(position + 1 + "");
            textViewTitle.setText(feedbackBean.getTitle());

            if (feedbackBean.getFeedbackType().equals(DATE)) {
                image_icon.setImageResource(R.mipmap.image_date);
            } else {
                image_icon.setImageResource(R.mipmap.image_selector_down);
            }
            if (feedbackBean.getRemark() != null && !feedbackBean.getRemark().equals("")) {
                layout_remark.setVisibility(View.VISIBLE);
                textViewRemark.setText(feedbackBean.getRemark());
            } else {
                layout_remark.setVisibility(View.GONE);
            }
            if (feedbackBean.getFeedbackContent() != null && feedbackBean.getFeedbackContent().getContent() != null) {
                textViewContent.setText(feedbackBean.getFeedbackContent().getContent());
            }
            layoutSelector.setOnClickListener(new MyOnClickListener(feedbackBean, textViewContent));
            if (status.equals(COMPLETED)) {
                layoutSelector.setEnabled(false);
            }
        }
        return view;
    }

    private class MyTextWatcher implements TextWatcher {
        FeedbackBean feedbackBean;

        public MyTextWatcher(FeedbackBean feedbackBean) {
            this.feedbackBean = feedbackBean;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (feedbackBean.getFeedbackContent() == null) {
                FeedbackContentBean feedbackContentBean = new FeedbackContentBean();
                feedbackContentBean.setFeedbackId(feedbackBean.getFeedbackId());
                feedbackContentBean.setItemId(feedbackBean.getItemId());
                feedbackBean.setFeedbackContent(feedbackContentBean);
            }
            feedbackBean.getFeedbackContent().setContent(s.toString());
            feedbackBean.getFeedbackContent().save();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    private class MyOnClickListener implements View.OnClickListener {
        private FeedbackBean feedbackBean;
        String[] strings;
        TextView textViewContent;
        MyDatePicker datePicker;

        public MyOnClickListener(FeedbackBean feedbackBean, TextView textViewContent) {
            this.feedbackBean = feedbackBean;
            this.textViewContent = textViewContent;
        }

        @Override
        public void onClick(View v) {
            if (feedbackBean.getFeedbackType().equals(LIST)) {
                if (feedbackBean.getFeedbackContent() != null && feedbackBean.getFeedbackContent().getSelectList() != null) {
                    List<SelectListBean> selectListBeanList = feedbackBean.getFeedbackContent().getSelectList();
                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < selectListBeanList.size(); i++) {
                        list.add(selectListBeanList.get(i).getValue());
                    }
                    strings = list.toArray(new String[list.size()]);

                    new AlertView("请选择", null, "取消", null, strings,
                            context, AlertView.Style.ActionSheet, new OnItemClickListener() {
                        public void onItemClick(Object o, int position) {
                            if (position != -1) {
                                if (feedbackBean.getFeedbackContent() == null) {
                                    FeedbackContentBean feedbackContentBean = new FeedbackContentBean();
                                    feedbackContentBean.setFeedbackId(feedbackBean.getFeedbackId());
                                    feedbackContentBean.setItemId(feedbackBean.getItemId());
                                    feedbackBean.setFeedbackContent(feedbackContentBean);
                                }
                                feedbackBean.getFeedbackContent().setContent(strings[position]);
                                textViewContent.setText(strings[position]);
                                feedbackBean.getFeedbackContent().save();
                            }
                        }
                    }).show();
                } else {
                    Toast.makeText(context, "未获取到列表数据", Toast.LENGTH_SHORT).show();
                }
            } else if (feedbackBean.getFeedbackType().equals(DATE)) {
                datePicker = new MyDatePicker(context,
                        new MyDatePicker.SubmitOperateDialogListener() {

                            @Override
                            public void onDateUnselected(Date date) {

                            }

                            @Override
                            public void onDateSelected(Date date) {

                                String selectDate = DateUtil
                                        .getDateStringFromDate(date);
                                textViewContent.setText(selectDate);
                                if (feedbackBean.getFeedbackContent() == null) {
                                    FeedbackContentBean feedbackContentBean = new FeedbackContentBean();
                                    feedbackContentBean.setFeedbackId(feedbackBean.getFeedbackId());
                                    feedbackContentBean.setItemId(feedbackBean.getItemId());
                                    feedbackBean.setFeedbackContent(feedbackContentBean);
                                }
                                feedbackBean.getFeedbackContent().setContent(selectDate);
                                feedbackBean.getFeedbackContent().save();

                                datePicker.dismiss();
                            }
                        });
                datePicker.setTitle("请点击选择日期");
                WindowManager wm = ((Activity) context).getWindowManager();

                int width = wm.getDefaultDisplay().getWidth();
                int height = wm.getDefaultDisplay().getHeight();

                datePicker.show();
                datePicker.getWindow().setLayout(width * 9 / 10,
                        height * 7 / 10);
            }
        }
    }
}
